package com.squirrel.michale;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/2/13 7:56 PM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode0144 {

    public List<Integer> preorderTraversal(TreeNode root) {

        List<Integer> integerList = new ArrayList<>();
        if (root != null) {
            Stack<TreeNode> stack = new Stack<TreeNode>();
            stack.add(root);
            while (!stack.isEmpty()) {
                root = stack.pop();
                integerList.add(root.val);
                if (root.right != null) {
                    stack.push(root.right);
                }
                if (root.left != null) {
                    stack.push(root.left);
                }
            }
        }
        return integerList;
    }
}
